package com.wy.music_player;

import com.wy.fragments.PersonInfoFragment;
import com.wy.fragments.SongsFragment;
import com.wy.service.MusicService;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 主界面，默认SongsFragment展示音乐目录
 */
public class MainActivity extends Activity{
	
	// UI 
    private TextView tab_songs;
    private TextView tab_person;
    private FrameLayout ly_content;
    // Fragment 
	private SongsFragment sfg;
	private PersonInfoFragment pfg;	
	private FragmentManager manager;
	// Account
	private String username,sex,country;
	private int id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		manager = getFragmentManager();
		setContentView(R.layout.fragement_layout);

		username = getIntent().getStringExtra("username");
		sex = getIntent().getStringExtra("sex");
		country = getIntent().getStringExtra("country");
		id = getIntent().getIntExtra("id",-1);
		if(id == -1)
			Log.i("MainActivity", "onCreate: id error");
		bindViews();
		tab_songs.performClick();// 先点击一次

		// 开启服务
		Intent it_service = new Intent(MainActivity.this,MusicService.class);
		startService(it_service);
	}

	// 绑定UI控件
	protected void bindViews(){
		tab_person = (TextView) findViewById(R.id.tab_person);
		tab_songs = (TextView) findViewById(R.id.tab_songs);
		ly_content = (FrameLayout) findViewById(R.id.ly_content);
		
		TabClickLinster clickLinster = new TabClickLinster();
		tab_person.setOnClickListener(clickLinster);
		tab_songs.setOnClickListener(clickLinster);
	}

	// 隐藏所有fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(sfg != null)fragmentTransaction.hide(sfg);
        if(pfg != null)fragmentTransaction.hide(pfg);
    }

    private void setSelected(){
        tab_person.setSelected(false);
        tab_songs.setSelected(false);        
    }

	/**
	 * 底部导航栏，点击监听
	 * 点击的时候先创建FragmentTransaction，置所有view为未点击
	 * 显示fragment
	 */
    class TabClickLinster implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			FragmentTransaction transaction = manager.beginTransaction();
			hideAllFragment(transaction);
			switch(v.getId()){
				case R.id.tab_songs:
					setSelected();
					tab_songs.setSelected(true);
					if(sfg == null){
						sfg = new SongsFragment();
						transaction.add(R.id.ly_content,sfg);						
					}else
						transaction.show(sfg);					
					break;
				case R.id.tab_person:
					setSelected();
					tab_person.setSelected(true);
					if(pfg == null){
						Bundle bundle = new Bundle();
						bundle.putString("username",username);
						bundle.putString("sex",sex);
						bundle.putSerializable("country",country);
						pfg = new PersonInfoFragment();
						pfg.setArguments(bundle);
						transaction.add(R.id.ly_content,pfg);						
					}else
						transaction.show(pfg);					
					break;
			}
			transaction.commit();
		}    	
    }
    
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
}
